module("gray_conf", package.seeall)

VERSION = "1.0"
VERSION_SUP = "1.x"
VERSION_NUM = 1000
VERSION_MAJOR = 1
VERSION_MIN= 0

ngx_conf_name = ngx.var.nginx_conf_name
if not ngx_conf_name or not string.match(ngx_conf_name, '^/') then 
    ngx_conf_name = "/opt/taobao/tengine/conf/tengine-proxy.conf"
end
ngx_bin_name = ngx.var.nginx_binary_name
if not ngx_bin_name or not string.match(ngx_bin_name, '^/') then
    ngx_bin_name = "/opt/taobao/tengine/bin/nginx"
end
ngx_pid_name = ngx.var.nginx_pid_name
if not ngx_pid_name or not string.match(ngx_pid_name, '^/') then
    ngx_pid_name = "/opt/taobao/tengine/logs/tengine-proxy.pid"
end
ngx_conf_prefix = ngx.var.nginx_conf_prefix
if not ngx_conf_prefix or not string.match(ngx_conf_prefix, '^/') then
    ngx_conf_prefix = "/opt/taobao/tengine/conf/"
end

ngx_query_loc = "/query"

if not string.match(ngx_conf_prefix, "/$") then
    ngx_conf_prefix = ngx_conf_prefix .. "/"
end

if not string.match(ngx_conf_name, "^/") then
    ngx_conf_name = ngx_conf_prefix .. ngx_conf_name
end

ngx_conf_test_name = ngx_conf_name .. ".test"
ngx_conf_swp_name = ngx_conf_prefix .. "." ..
                    string.match(ngx_conf_name, "/([^/]+/?)$") .. ".swp"

ngx_user_name = ngx.var.nginx_user_name
if not ngx_user_name then
    ngx_user_name = "admin"
end

ngx_group_name = ngx.var.nginx_group_name
if not ngx_group_name then
    ngx_group_name = "admin"
end

gray_strategy_dirname = "gray_strategy/"
gray_strategy_pathname = ngx_conf_prefix .. gray_strategy_dirname
gray_conf_dirname = "gray_conf/"
gray_conf_pathname = ngx_conf_prefix .. gray_conf_dirname
gray_resource_dirname = "resource/"
gray_lua_script_dirname = "lua_script/"
gray_error_dirname = "error_confs/"
gray_backup_dirname = "backup/"
gray_json_dirname = "json/"

gray_sid_var_name = "gray_sid"
gray_server_var_name = "gray_server_name"
gray_uri_var_name = "gray_uri"

gray_lua_srcipt_template_dirname = "lua_script/template/"

gray_tair_script_basename = "gray_tair.lua"

engine_enable_basename = "engine_enable.lua"
engine_conf_basename = "engine_conf.lua"

engine_init_script_basename = "engine_init.lua"
engine_init_script_fullname = gray_conf_pathname .. gray_lua_script_dirname ..
                       engine_init_script_basename

engine_script_template_basename = "gray_engine.template"
engine_script_template_fullname = gray_conf_pathname .. gray_lua_srcipt_template_dirname ..
                                  engine_script_template_basename

engine_script_basename = "gray_engine.lua"
engine_script_fullname = gray_conf_pathname .. gray_lua_script_dirname ..
                         engine_script_basename

gray_http_inc_basename = "gray_http_inc.conf"
gray_srv_inc_basename = "gray_srv_inc.conf"

gray_resource_pathname = gray_conf_pathname .. gray_resource_dirname
gray_backup_pathname = gray_conf_pathname .. gray_backup_dirname
gray_json_pathname = gray_conf_pathname .. gray_json_dirname

original_file_map_basename = "original_file_map"

engine_script_basename = "gray_engine.lua"

gray_error_pathname = gray_conf_pathname .. gray_error_dirname

gray_tair_script_template_basename = "gray_tair.template"
gray_tair_template_fullname = gray_conf_pathname .. gray_lua_srcipt_template_dirname ..
                              gray_tair_script_template_basename

-- after so many times, if nginx still not reloaded,
-- we will recover to original configure. 150 * 0.1 = 15s
gray_reload_retry_times = 100

-- for cell
cell_server_conf = "cell_server.conf"
cell_upstream_variable = "ups"


function get_gray_sid_path(sid)
    return string.format("%sgray_%s/", gray_strategy_pathname, tostring(sid))
end

function get_gray_sid_dir(sid)
    return string.format("gray_%s/", tostring(sid))
end

function get_engine_enable_fullname(sid)
    return get_gray_sid_path(sid) .. engine_enable_basename
end

function get_engine_enable_module_name(sid)
    return string.format("gray_%s.engine_enable", tostring(sid))
end

function get_engine_conf_module_name(sid)
    return string.format("gray_%s.engine_conf", tostring(sid))
end

function get_engine_module_name(sid)
    return string.format("gray_%s.gray_engine", tostring(sid))
end

function get_engine_module_name_from_path(path)
    return string.format("%s.gray_engine", tostring(path))
end

function get_engine_conf_module_name_from_path(path)
    return string.format("%s.engine_conf", tostring(path))
end

function get_tair_module_name(sid)
    return string.format("gray_%s.gray_tair", tostring(sid))
end

function get_engine_conf_fullname(sid)
    return get_gray_sid_path(sid) .. engine_conf_basename
end

function get_gray_json_fullname(sid)
    return gray_json_pathname .. string.format("gray_%s_conf.json", tostring(sid))
end

function get_gray_json_basename(sid)
    return string.format("gray_%s_conf.json", tostring(sid))
end

function get_http_inc_fullname(sid)
    return get_gray_sid_path(sid) .. gray_http_inc_basename
end

function get_srv_inc_fullname(sid)
    return get_gray_sid_path(sid) .. gray_srv_inc_basename
end

function get_server_name(sid, seq)
    if seq then
        return string.format("gray_%s_%s_sb", tostring(sid), tostring(seq))
    else
        return string.format("gray_%s_sa", tostring(sid))
    end
end

function get_geo_block_name(sid, seq)
    return string.format("gray_%s_%s_ip", tostring(sid), tostring(seq))
end

function get_geo_city_fullname(city)
    return gray_resource_pathname .. string.format("geo_%s.conf", city)
end

function get_etair_name(sid)
    return string.format("gray_%s_group", tostring(sid))
end

function get_engine_script_fullname(sid)
    return get_gray_sid_path(sid) .. engine_script_basename
end

function get_gray_tair_script_fullname(sid)
    return get_gray_sid_path(sid) .. gray_tair_script_basename
end

function get_original_file_map_fullname(sid)
    return get_gray_sid_path(sid) .. original_file_map_basename 
end

function version_to_num(version)
    local major, min = string.match(version, "^(%d+)%.(%d+)$")
    major = tonumber(major)
    min = tonumber(min)
    if not major or not min then
        return nil, "unknown version: "..version
    end
    return major * 1000 + min, major, min
end

function get_gray_tair_loc_name(sid)
    return string.format("/gray_tair_%s_loc", tostring(sid))
end


getmetatable(gray_conf).__newindex = function (table, key, val)
    error('Attempt to write to undeclared variable "' .. key .. '": '
          .. debug.traceback())
end

